package se.su.dsv.theclient;

import theserver.Venue;
import theserver.VenueActivity;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SocialActivityList extends ListActivity {
	VenueActivity activity = null;
	Venue venue = null;
	private boolean serviceBound;
	protected ISocialService m_service;
	private String[] activities = new String[]{};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//venue = getIntent().getData();
		if (!serviceBound) {
			bindService();
			try {
				int list = m_service.getActivityList(venue.getVenueName(), activities);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, activities));
		getListView().setTextFilterEnabled(true);
	}

	//    static final String[] ACTIVITIES = new String[] {
	//        "Fika", "Bärsa", "Kolla på Fotboll"
	//      };

	public void onListItemClick(ListView l, View v, int position, long id) {
		//getListView().getItemAtPosition(position);
		super.onListItemClick(l, v, position, id);
		Intent i = new Intent(this, SocialVenueList.class);
		//i.putExtra(NotesDbAdapter.KEY_ROWID, id);
		//startActivityForResult(i, ACTIVITY_EDIT);
		startActivity(i);
	}

	private void bindService() {
		serviceBound = bindService(new Intent(this, SocialService.class), serviceConnection, Context.BIND_AUTO_CREATE);
	}

	private ServiceConnection serviceConnection = new ServiceConnection(){

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Toast.makeText(SocialActivityList.this, R.string.service_connected, Toast.LENGTH_SHORT).show();
			m_service = ISocialService.Stub.asInterface(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			Toast.makeText(SocialActivityList.this, R.string.service_disconnected, Toast.LENGTH_SHORT).show();		
		}

	};
}
